package com.my.basic.classes;

import java.util.Objects;

/**
 * @author marshal
 * @version 2021-06-02
 */
public class EqualsTest {
    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = "abc";
        System.out.println(str1.equals(str2)); // true

        // 不重写hashCode()和equals则返回false;重写hashCode()和equals(),当不同的User对象的姓名，年龄，身份证号相同是就判断为true
        System.out.println(new User(10, "Tom", "20010101").equals(new User(10, "Tom", "20010101")));


    }

    static class User {
        private int age;
        private String name;
        private String idCard;

        public User(int age, String name, String idCard) {
            this.age = age;
            this.name = name;
            this.idCard = idCard;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            User user = (User) o;
            return age == user.age && Objects.equals(name, user.name) && Objects.equals(idCard, user.idCard);
        }

        @Override
        public int hashCode() {
            return Objects.hash(age, name, idCard);
        }
    }
}
